package com.ubisolv.vmakeup.view;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ubisolv.vmakeup.MainActivity;
import com.ubisolv.vmakeup.R;
import com.ubisolv.vmakeup.TakePictureLActivity;
import com.ubisolv.vmakeup.TakePicturePActivity;
import com.ubisolv.vmakeup.app.Const;
import com.ubisolv.vmakeup.util.ResourceUtil;

public class SourceSelectView extends ListView {

	private static final String ITEM =			"item";
	private static final String ITEM_ID =		"id";
	private static final String ITEM_TITLE =	"title";
	private static final String ITEM_DESC =		"desc";
	
	private Context context;
	private List<Map<String, String>> items;

	public SourceSelectView(Context context) {
		super(context);
		
		init(context);
	}

	public SourceSelectView(Context context, AttributeSet atts) {
		super(context, atts);
		
		init(context);
	}
	
	private void init(Context context) {
		this.context =	context; 

		setAdapter(createListAdapter());
		setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				selectedOnItem(arg0, arg1, arg2, arg3);
			}
		});
	}

	private BaseAdapter createListAdapter() {
		final String[] fromMapKey =	new String[] {ITEM_TITLE, ITEM_DESC};
		final int[] toLayoutId =	new int[] {android.R.id.text1, android.R.id.text2};
		items =						ResourceUtil.getListResource(context, R.xml.source_select, ITEM);
		
		/* logging data
		for (int i = 0; i < list.size(); i++) {
			Log.d("source select data", list.get(i).toString());
		}
		*/
		
		return new SimpleAdapter(context, items, android.R.layout.simple_list_item_2, fromMapKey, toLayoutId);
	}
	
	private void selectedOnItem(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		if (items == null && arg2 >= items.size()) {
			throw new IllegalStateException("items is null or list index is out of bound.");
		}
		
		Map<String, String> target	=	items.get(arg2);
		
		if ("01".equals(target.get(ITEM_ID))) {
			takePicture();
		} else 
		if ("02".equals(target.get(ITEM_ID))) {
			selectPicture();
			
		}
		
		Log.d("selectedOnItem", Integer.toString(arg2));
	}
	
	private void takePicture() {
		//Intent intent =	new Intent(context, TakePictureLActivity.class);
		Intent intent =	new Intent(context, TakePicturePActivity.class);
		((Activity)context).startActivity(intent);
	}
	
	private void selectPicture() {
		Intent intent =	new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		((Activity)context).startActivityForResult(intent, Const.Events.EVENT_SELECT_PICTURE);
	}
	
}
